export const clearSessionStorage = () => {
  sessionStorage.removeItem('account')
  sessionStorage.removeItem('token')
  sessionStorage.removeItem('oss')
  sessionStorage.removeItem('router')
}
const r = []
export const getRouters = router => {
  router.map(i => {
    if (i.menu) {
      getRouters(i.menu)
    } else {
      r.push({component: i.component, path: i.path, name: i.name})
    }
  })
  return r
}

export const getBubbleSort = (attr,rev)=>{
  if( rev===undefined ){ rev=1 }else{ (rev)?1:-1; }
  return function (a,b){
    a=a[attr];
    b=b[attr];
    if(a<b){ return rev*-1}
    if(a>b){ return rev* 1 }
    return 0;
  }
}


export const compare = function (prop) {
  return function (obj1, obj2) {
    let val1 = obj1[prop];
    let val2 = obj2[prop];
    if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
      val1 = Number(val1);
      val2 = Number(val2);
    }
    if (val1 < val2) {
      return -1;
    } else if (val1 > val2) {
      return 1;
    } else {
      return 0;
    }
  }
}

const browserList = [
  {
    browser: 'Edge',
    versions: 79
  },
  {
    browser: 'Firefox',
    versions: 78
  },
  {
    browser: 'Chrome',
    versions: 86
  },
  {
    browser: 'Safari',
    versions: 12
  }
]


function getBrowserVersion(browserType, UserAgent) {
  let versions = '';

  switch (browserType) {
    case 'IE':
      versions = UserAgent.match(/(msie\s|trident.*rv:)([\w.]+)/)[2];
      break;
    case 'Chrome':
      for (let mt in navigator.mimeTypes) {
        //360 pc
        if (navigator.mimeTypes[mt]['type'] == 'application/360softmgrplugin') {
          browserType = '360';
        }
      }
      versions = UserAgent.match(/chrome\/([\d.]+)/)[1];
      break;

    case 'Firefox':
      versions = UserAgent.match(/firefox\/([\d.]+)/)[1];
      break;
    case 'Opera':
      versions = UserAgent.match(/opera\/([\d.]+)/)[1];
      break;
    case 'Safari':
      versions = UserAgent.match(/version\/([\d.]+)/)[1];
      break;
    case 'Edge':
      versions = UserAgent.match(/edge\/([\d.]+)/)[1];
      break;
    case 'QQBrowser':
      versions = UserAgent.match(/qqbrowser\/([\d.]+)/)[1];
      break;
  }

  return parseInt(versions);
}

function getBrowser() {
  const UserAgent = navigator.userAgent.toLowerCase();
  const browserInfo = {};

  const browserObj = {
    IE: window.ActiveXObject || "ActiveXObject" in window,
    Chrome: UserAgent.indexOf('chrome') > -1 && UserAgent.indexOf('safari') > -1,
    Firefox: UserAgent.indexOf('firefox') > -1,
    Opera: UserAgent.indexOf('opera') > -1,
    Safari: UserAgent.indexOf('safari') > -1 && UserAgent.indexOf('chrome') == -1,
    Edge: UserAgent.indexOf('edge') > -1,
    QQBrowser: /qqbrowser/.test(UserAgent),
    WeixinBrowser: /MicroMessenger/i.test(UserAgent)
  };

  for (let key in browserObj) {
    if (browserObj.hasOwnProperty(key) && browserObj[key]) {
      browserInfo.browser = key;
      browserInfo.versions = getBrowserVersion(key, UserAgent);
      break
    }
  }

  return browserInfo;
}

export function isUpgradeBrowser() {
  const { browser, versions } = getBrowser()

  for (const objKey of browserList) {
    if (objKey.browser === browser && versions < objKey.versions) {
      // 需要升级
      return {
        browser,
        versions,
        lowestVersions: objKey.versions,
        isUpdate: true
      }
    }
  }

  // 默认不需要升级
  return {
    browser,
    versions,
    isUpdate: false
  }
}




